//
//  ZDTableHeadView.m
//  1012product展示02
//
//  Created by Apple on 15/10/12.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDTableHeadView.h"
#import "ZDSubjectCollectionCell.h"

@interface ZDTableHeadView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation ZDTableHeadView


+ (instancetype)tableHeadView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"ZDTableHeadView" owner:nil options:nil] lastObject];
}


- (void)awakeFromNib
{
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDSubjectCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ZDSubjectCollectionCell"];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
//    ZDLog(@"layout:%@-----%@", layout, NSStringFromCGRect(self.collectionView.frame));
    
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    //设置cell的size
    layout.itemSize = CGSizeMake(150, 60);
    
    layout.minimumInteritemSpacing = 10;
    
    self.collectionView.collectionViewLayout = layout;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //设置滚动方向为水平 ———— 奇怪，在xib中设置没有效果
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //隐藏水平滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZDSubjectCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZDSubjectCollectionCell" forIndexPath:indexPath];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [[UIViewController alloc] init];
    
    vc.view.backgroundColor = [UIColor orangeColor];
    
    if (self.itemClickBlock)
    {
        self.itemClickBlock(vc);
    }
}

#pragma mark - 通过UICollectionViewDelegateFlowLayout代理方法设置layout布局参数
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(150, 60);
//}
//
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(0, 10, 0, 10);
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 10;
//}

@end
